package net.hn.hnms.biz.equipment.company.service;

import net.hn.hnms.biz.equipment.domain.vo.*;
import net.hn.hnms.biz.equipment.domain.vo.*;
import net.hn.hnms.biz.equipment.link.SteelyardNowLinkService;
import net.hn.hnms.biz.equipment.mapper.CoalMineSysPointMapper;
import net.hn.hnms.biz.equipment.until.MineName;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 智呈物联 nzw
 * @date 2023/2/2
 */
@Service
public class QueryStatsService {

    private MineName mineName;

    private SteelyardNowLinkService steelyardNowLinkService;

    @Resource
    private CoalMineSysPointMapper coalMineSysPointMapper;

    @Autowired
    public void setMineName(MineName mineName) {
        this.mineName = mineName;
    }

    @Autowired
    public void setSteelyardNowLinkService(SteelyardNowLinkService steelyardNowLinkService) {
        this.steelyardNowLinkService = steelyardNowLinkService;
    }

    private List<String> setWeekData(){
        List<String> weekList = new ArrayList<>();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_YEAR, -8);
        for (int i=0;i<6;i++){
            calendar.add(Calendar.DAY_OF_YEAR, 1);
            weekList.add(simpleDateFormat.format(calendar.getTime()));
        }

        return weekList;
    }

    private List<String> setWeekValue(List<Map<String,Object>> valueList,List<String> weekData){
        List<String> weekValue = new ArrayList<>();
        Map<String,Object> middleValue = new HashMap<>();
        valueList.forEach(value ->{
            middleValue.put(value.get("dataTime")+"",value.get("count"));
        });
        weekData.forEach(data->{
            String value = middleValue.get(data)==null?"0":middleValue.get(data)+"";
            weekValue.add(value);
        });
        return weekValue;
    }

    public List<DrainageStatisticsVO> DrainageStatistics(){
        List<Map<String,Object>> list = coalMineSysPointMapper.selectDrainageCount();
        List<DrainageStatisticsVO> drainageStatisticsVOList = new ArrayList<>();
        list.forEach(map -> {
            DrainageStatisticsVO drainageStatisticsVO = new DrainageStatisticsVO();
            String mineCode = map.get("mine_code")+"";
            //煤矿编码及名称
            drainageStatisticsVO.setMineCode(mineCode);
            drainageStatisticsVO.setMineName(mineName.getMineName(mineCode));

            Long count = Long.parseLong(map.get("count")+"");
            //总数量
            drainageStatisticsVO.setPumpNum(count);

            Map<String,Long> normalOperationCount = coalMineSysPointMapper.drainageNormalOperationCount(mineCode);
            Long runningPump = normalOperationCount.get("count");
            //正常运行数量
            drainageStatisticsVO.setRunningPump(runningPump);
            //报警数量
            drainageStatisticsVO.setAlarmPump(count-runningPump);

            List<String> weekData = setWeekData();
            List<Map<String,Object>> normalOperationCountList = coalMineSysPointMapper.drainageNormalOperationCountByTime(mineCode,weekData.get(0));
            normalOperationCountList.forEach(norm->{
                norm.put("count",count-Long.parseLong(norm.get("count")+""));
            });
            List<String> weekValue = setWeekValue(normalOperationCountList,weekData);

            //设置最新数据
            weekValue.add((count-runningPump)+"");
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            weekData.add(simpleDateFormat.format(new Date()));

            //折线y轴数据列表
            drainageStatisticsVO.setYData(weekValue);
            //折线x轴时间列表
            drainageStatisticsVO.setXData(weekData);

            drainageStatisticsVOList.add(drainageStatisticsVO);
        });
        return drainageStatisticsVOList;
    }

    public List<FanStatisticsVO> fanStatistics(){
        List<Map<String,Object>> list = coalMineSysPointMapper.fanStatistics();
        List<FanStatisticsVO> fanStatisticsVOList = new ArrayList<>();
        list.forEach(map -> {
            FanStatisticsVO fanStatisticsVO = new FanStatisticsVO();
            String mineCode = map.get("mine_code")+"";
            //煤矿名称以及编码
            fanStatisticsVO.setMineCode(mineCode);
            fanStatisticsVO.setMineName(mineName.getMineName(mineCode));
            Integer count = Integer.parseInt(map.get("count")+"");
            //总数量
            fanStatisticsVO.setVentilatorNum(count);
            Map<String,Long> normalOperationCount = coalMineSysPointMapper.fanNormalOperationCount(mineCode);
            Long runningVentilator = normalOperationCount.get("count");
            //正常运行数量
            fanStatisticsVO.setRunningVentilator(runningVentilator);
            //报警数量
            fanStatisticsVO.setAlarmVentilator(count-runningVentilator);
            //功率
            fanStatisticsVO.setPower("");

            List<String> weekData = setWeekData();
            List<Map<String,Object>> fanNormalOperationCountByTimeList = coalMineSysPointMapper.fanNormalOperationCountByTime(mineCode,weekData.get(0));
            fanNormalOperationCountByTimeList.forEach(norm->{
                norm.put("count",count-Long.parseLong(norm.get("count")+""));
            });
            List<String> weekValue = setWeekValue(fanNormalOperationCountByTimeList,weekData);

            //设置最新数据
            weekValue.add((count-runningVentilator)+"");
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            weekData.add(simpleDateFormat.format(new Date()));
            fanStatisticsVO.setXData(weekData);
            fanStatisticsVO.setYData(weekValue);

            fanStatisticsVOList.add(fanStatisticsVO);
        });

        return fanStatisticsVOList;
    }

    public List<InclineShaftStatisticsVO> inclineShaftStatistics(){
        List<InclineShaftStatisticsVO> inclineShaftStatisticsVOList = new ArrayList<>();

        List<Map<String,Object>> list = coalMineSysPointMapper.inclineStatistics();
        list.forEach(map -> {
            InclineShaftStatisticsVO inclineShaftStatisticsVO = new InclineShaftStatisticsVO();
            String mineCode = map.get("mine_code")+"";
            //煤矿名称
            inclineShaftStatisticsVO.setMineCode(mineCode);
            inclineShaftStatisticsVO.setMineName(mineName.getMineName(mineCode));

            inclineShaftStatisticsVOList.add(inclineShaftStatisticsVO);
        });

        return inclineShaftStatisticsVOList;
    }

    public List<CoalflowStatisticsVO> coalFlowShaftStatistics(){

        return new ArrayList<>();
    }

    public List<ThermalshockStatisticsVO> thermalShockShaftStatistics(){
        List<ThermalshockStatisticsVO> inclineShaftStatisticsVOList = new ArrayList<>();

        List<Map<String,Object>> list = coalMineSysPointMapper.thermalShockShaftStatistics();
        list.forEach(map -> {
            ThermalshockStatisticsVO thermalshockStatisticsVO = new ThermalshockStatisticsVO();
            Integer count = Integer.parseInt(map.get("count")+"");
            String mineCode =  map.get("mine_code")+"";

            thermalshockStatisticsVO.setMineCode(mineCode);
            thermalshockStatisticsVO.setMineName(mineName.getMineName(mineCode));

            Double stellyard = steelyardNowLinkService.nowLink(mineCode).getData();
            Double maxStellyard = steelyardNowLinkService.maxForYearLink(mineCode).getData();

            //日累计产量
            thermalshockStatisticsVO.setOutput(stellyard+"");

            thermalshockStatisticsVO.setMaxOutput(maxStellyard+"");

            thermalshockStatisticsVO.setNum(count);
            List<Map<String,Object>> normalOperationCount = coalMineSysPointMapper.thermalShockAlarmCount(mineCode);
            Integer alarm = 0;
            for (Map<String,Object> normalOperationCountMap : normalOperationCount) {
                alarm = alarm + Integer.parseInt(normalOperationCountMap.get("count") + "");
            }
            thermalshockStatisticsVO.setAlarm(alarm);


            List<String> weekData = setWeekData();
            List<Map<String,Object>> fanNormalOperationCountByTimeList = coalMineSysPointMapper.thermalShockAlarmCountByTime(mineCode,weekData.get(0));
            fanNormalOperationCountByTimeList.forEach(norm->{
                norm.put("count",Long.parseLong(norm.get("count")+""));
            });
            List<String> weekValue = setWeekValue(fanNormalOperationCountByTimeList,weekData);

            //设置最新数据
            weekValue.add(alarm+"");
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            weekData.add(simpleDateFormat.format(new Date()));
            thermalshockStatisticsVO.setXData(weekData);
            thermalshockStatisticsVO.setYData(weekValue);

            inclineShaftStatisticsVOList.add(thermalshockStatisticsVO);
        });
        return inclineShaftStatisticsVOList;
    }

    public List<DrivageStatisticsVO> drivageShaftStatistics(){
        List<DrivageStatisticsVO> inclineShaftStatisticsVOList = new ArrayList<>();

        List<Map<String,Object>> list = coalMineSysPointMapper.drivageShaftStatistics();
        list.forEach(map -> {
            DrivageStatisticsVO drivageStatisticsVO = new DrivageStatisticsVO();
            Integer count = Integer.parseInt(map.get("count")+"");
            String mineCode =  map.get("mine_code")+"";

            drivageStatisticsVO.setMineCode(mineCode);
            drivageStatisticsVO.setMineName(mineName.getMineName(mineCode));

            drivageStatisticsVO.setNum(count);
            List<Map<String,Object>> normalOperationCount = coalMineSysPointMapper.drivageShaftAlarmCount(mineCode);
            Integer alarm = Integer.parseInt(normalOperationCount.get(0).get("count")+"");
            drivageStatisticsVO.setAlarm(alarm);

            List<String> weekData = setWeekData();
            List<Map<String,Object>> drivageShaftAlarmCountByTime = coalMineSysPointMapper.drivageShaftAlarmCountByTime(mineCode,weekData.get(0));
            drivageShaftAlarmCountByTime.forEach(norm->{
                norm.put("count",Long.parseLong(norm.get("count")+""));
            });
            List<String> weekValue = setWeekValue(drivageShaftAlarmCountByTime,weekData);

            //设置最新数据
            weekValue.add(alarm+"");
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            weekData.add(simpleDateFormat.format(new Date()));
            drivageStatisticsVO.setXData(weekData);
            drivageStatisticsVO.setYData(weekValue);


            inclineShaftStatisticsVOList.add(drivageStatisticsVO);
        });
        return inclineShaftStatisticsVOList;
    }
}
